package me.project.jobster.controller;

import me.project.jobster.common.constant.GlobalConstants;
import me.project.jobster.dao.mybatis.model.Company;
import me.project.jobster.dao.mybatis.model.Position;
import me.project.jobster.dao.mybatis.model.Student;
import me.project.jobster.pojo.Applications;
import me.project.jobster.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Mcdull
 * @date 2018-4-27
 */
@Controller
@RequestMapping("user")
public class DashboardController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private JobService jobService;
    @Autowired
    private FollowService followService;
    @Autowired
    private FriendService friendService;
    @Autowired
    private MessageService messageService;

    /**
     * student profile page
     *
     * @param model
     * @return
     */
    @RequestMapping("profile/index")
    public String index(HttpServletRequest request, Model model) {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        String userFlag = String.valueOf(session.getAttribute("user_flag"));
        String userId = String.valueOf(session.getAttribute("user_id"));
        if (userFlag.equals(GlobalConstants.STUDENT)) {
            session.setAttribute(GlobalConstants.MENU, "00");
            Student student = userService.getOwnerInfo(userId);
            model.addAttribute("student", student);
            return "student/profile";
        } else {
            session.setAttribute(GlobalConstants.MENU, "10");
            Company company = userService.getCompanyInfo(userId);
            model.addAttribute("company", company);
            return "company/profile";
        }
    }


    /**
     * student profile page
     *
     * @param model
     * @return
     */
    @PostMapping("profile/update")
    public String update(HttpServletRequest request, Model model) {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        String userFlag = String.valueOf(session.getAttribute("user_flag"));
        String userId = String.valueOf(session.getAttribute("user_id"));
        if (userFlag.equals(GlobalConstants.STUDENT)) {
            String userName = request.getParameter("name");
            String university = request.getParameter("university");
            String major = request.getParameter("major");
            String gpa = request.getParameter("gpa");
            String gpaStatus = request.getParameter("gpa_status");
            String resume = request.getParameter("resume");
            userService.updateStudentProfile(userId, userName, university, major, gpa, gpaStatus, resume);

            session.setAttribute("user_name", userName);
            Student student = userService.getOwnerInfo(userId);
            model.addAttribute("student", student);
            return "student/profile";
        } else {
            String userName = request.getParameter("name");
            String location = request.getParameter("location");
            String headquarter = request.getParameter("headquarter");
            String industry = request.getParameter("industry");
            String remark = request.getParameter("remark");
            userService.updateCompanyProfile(userId, userName, location, headquarter, industry, remark);

            session.setAttribute("user_name", userName);
            Company company = userService.getCompanyInfo(userId);
            model.addAttribute("company", company);
            return "company/profile";
        }
    }

    //---------------------------company-----------------------------//

    /**
     * my post page
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("job/posted")
    public String myPost(HttpServletRequest request, Model model) {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        String userId = String.valueOf(session.getAttribute("user_id"));
        session.setAttribute(GlobalConstants.MENU, "11");

        List<Position> list = jobService.getJobListConditional(userId);
        model.addAttribute("positionList", list);
        model.addAttribute("position", new Position());
        return "company/my_post";
    }

    /**
     * my post page
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("follower")
    public String myFollower(HttpServletRequest request, Model model) {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        String userId = String.valueOf(session.getAttribute("user_id"));
        session.setAttribute(GlobalConstants.MENU, "12");

        List<Student> list = followService.getFollower(userId);
        model.addAttribute("studentList", list);
        model.addAttribute("student", new Student());
        return "company/my_follower";
    }

    /**
     * Applications received
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("application/received")
    public String application(HttpServletRequest request, Model model) {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        String userId = String.valueOf(session.getAttribute("user_id"));
        session.setAttribute(GlobalConstants.MENU, "13");

        List<Applications> list = messageService.getApplications(userId);
        model.addAttribute("applications", list);
        return "company/applications";
    }



    //---------------------------student-----------------------------//


    /**
     * my follow page
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/follow")
    public String myFollow(HttpServletRequest request, Model model) {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        String userId = String.valueOf(session.getAttribute("user_id"));
        session.setAttribute(GlobalConstants.MENU, "01");

        List<Company> list = followService.getMyFollow(userId);
        model.addAttribute("companyList", list);
        model.addAttribute("company", new Company());
        return "student/my_follow";
    }

    /**
     * my friend page
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/friend")
    public String myfriend(HttpServletRequest request, Model model) {
        countMsgUnread(request);

        HttpSession session = request.getSession();
        String userId = String.valueOf(session.getAttribute("user_id"));
        session.setAttribute(GlobalConstants.MENU, "02");

        //remove itself
        String userFlag = String.valueOf(session.getAttribute("user_flag"));
        List<Student> list = friendService.getAllFriend(userId);

        if (GlobalConstants.STUDENT.equals(userFlag)) {
            for (Student student : list) {
                if (student.getsId().equals(userId)) {
                    list.remove(student);
                    break;
                }
            }
        }
        model.addAttribute("studentList", list);
        model.addAttribute("student", new Student());
        return "student/my_friend";
    }
}
